package com.engine.init;

import com.alibaba.fastjson.JSONObject;
import com.engine.common.util.SimpleReadAndWriteFileUtil;
import com.engine.init.entity.*;
import lombok.extern.slf4j.Slf4j;
import lombok.var;
import org.geolatte.geom.M;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
@Slf4j
public class InitDataList {



    public static JdbcTemplate jdbcTemplate;

    @PostConstruct
    public static String initAndRefresh() {
        log.info("获取表信息已执行");
        log.debug("debug打印启动");
        List<TablePropertieEntity> list = jdbcTemplate.query("SELECT id,table_name,table_type,general_columns,gis_columns," +
                        "detail_type,sys_columns,igrone_columns,description,minz,maxz,source_style,layer_style,srid,zoom_control,permission_control" +
                        " FROM sys_table_properties AS pp LEFT JOIN geometry_columns AS gg ON pp.table_name=gg.f_table_name"
                ,new BeanPropertyRowMapper<>(TablePropertieEntity.class));
        Globle.TablePropertiesMap = new HashMap<>();
        for (TablePropertieEntity tpe : list) {
            Globle.TablePropertiesMap.put(tpe.getTableName(),tpe);
        }
        log.info("加载数据表：" + Globle.TablePropertiesMap.size());
        Globle.GroupPropertiesMap=new HashMap<>();
        List<GroupPropertiesEntity> listGroupPropertiesEntity=jdbcTemplate.query("SELECT id,group_name,group_type,group_style,description FROM " +
                        "sys_group_properties",
                new BeanPropertyRowMapper<GroupPropertiesEntity>(GroupPropertiesEntity.class));
        for (var groupPropertiesEntity:listGroupPropertiesEntity) {
            List<TableToGroupEntity> list1= jdbcTemplate.query("SELECT id,zmin,zmax,style,description,layer_name,table_name,group_name FROM sys_table_to_group  " +
                    "WHERE group_name='" + groupPropertiesEntity.getGroupName() + "'",new BeanPropertyRowMapper<>(TableToGroupEntity.class));
            groupPropertiesEntity.setListTableToGroupEntity(list1);
            Map<String,TableToGroupEntity> m=new HashMap<>();
            for (TableToGroupEntity table:list1) {
                m.put(table.getTableName(),table);
            }
            groupPropertiesEntity.setMapTableToGroupEntity(m);
            Globle.GroupPropertiesMap.put(groupPropertiesEntity.getGroupName(),groupPropertiesEntity);
        }
        log.info("加载组数据：" + Globle.GroupPropertiesMap.size());
        List<TableStatisticalPropertiesEntity> list2 = jdbcTemplate.query("SELECT id,statistical_name,table_name,description,sql,table_id" +
                " FROM sys_table_statistical_properties",new BeanPropertyRowMapper<>(TableStatisticalPropertiesEntity.class));
        Globle.TableStatisticalPropertiesMap = new HashMap<>();
        for (var tsp: list2) {
            Globle.TableStatisticalPropertiesMap.put(tsp.getStatisticalName(), tsp);
        }
        log.info("加统计内容：" + Globle.TableStatisticalPropertiesMap.size());
        Globle.PacRelation=new HashMap<String, List<String>>();
        String s= SimpleReadAndWriteFileUtil.ReadFileInResource("/data/location/ShiXianRelationJson.json");
        Globle.PacRelation=(Map)JSONObject.parse(s);
        return "加载数据表：" + Globle.TablePropertiesMap.size()+"|加载组数据：" + Globle.GroupPropertiesMap.size()
                +"|加统计内容：" + Globle.TableStatisticalPropertiesMap.size();

    }

    @Autowired
    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        InitDataList.jdbcTemplate = jdbcTemplate;
    }

}
